package com.kudian.restful.controller;

import com.kudian.common.exception.ApplicationException;
import com.kudian.common.persistence.ResultObj;
import com.kudian.common.utils.JsonUtils;
import com.kudian.restful.service.AccountStatementService;
import com.kudian.restful.service.AdService;
import com.kudian.restful.service.LoginSessionService;
import com.kudian.restful.vo.accountstatement.AccountStatementVO;
import com.kudian.restful.vo.accountstatement.ConfirmAccountStatementBySellerVO;
import com.kudian.restful.vo.accountstatement.QueryAccountStatementBySellerRetVO;
import com.kudian.restful.vo.accountstatement.QueryAccountStatementBySellerVO;
import com.kudian.restful.vo.category.CategoryInfoRetVO;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by apple on 16/7/10.
 */
@RestController
@RequestMapping("accountStatement")
public class AccountStatementController {

    private static final Logger logger = Logger.getLogger(AccountStatementController.class);

    @Autowired
    private AccountStatementService accountStatementService;

    @Autowired
    private LoginSessionService loginSessionService;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ApiOperation(value = "每日对账", httpMethod = "POST", notes = "每日对账", response = ResultObj.class)
    public ResultObj add(@RequestBody AccountStatementVO vo) {
        logger.info("add=>" + JsonUtils.toJson(vo));
        ResultObj o = new ResultObj();
        try {
            o = this.accountStatementService.add(vo);
//            o.setErrcode(0);
//            o.setErrmsg("对账成功");
        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("对账失败");
        }

        return o;
    }

    @RequestMapping(value = "queryBySeller", method = RequestMethod.POST)
    @ApiOperation(value = "商家查询对账单", httpMethod = "POST", notes = "商家查询对账单", response = QueryAccountStatementBySellerRetVO.class)
    public QueryAccountStatementBySellerRetVO queryBySeller(@RequestBody QueryAccountStatementBySellerVO vo) {
        QueryAccountStatementBySellerRetVO o = new QueryAccountStatementBySellerRetVO();
        try {
            o.setItems(this.accountStatementService.queryBySeller(vo));
            o.setErrcode(0);
            o.setErrmsg("商家查询对账单成功");
        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("商家查询对账单失败");
        }
        return o;
    }

    @RequestMapping(value = "confirm", method = RequestMethod.POST)
    @ApiOperation(value = "商家确认账单", httpMethod = "POST", notes = "商家确认账单", response = ResultObj.class)
    public ResultObj confirm(@RequestBody ConfirmAccountStatementBySellerVO vo) {
        ResultObj o = new ResultObj();
        try {
            accountStatementService.confirm(vo);
            o.setErrcode(0);
            o.setErrmsg("商家确认账单成功");
        } catch (ApplicationException e) {
            logger.error(e);
            o.setErrcode(e.getErrcode());
            o.setErrmsg(e.getErrmsg());
        } catch (Exception e) {
            logger.error(e);
            o.setErrcode(9999);
            o.setErrmsg("商家确认账单失败");
        }
        return o;
    }
}
